package com.lancoo.znbkxx.teacher.utils;

import android.os.SystemClock;
import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class TimeUtils {

    /**
     * 服务器返回的时间格式
     */
    public static final String SERVER_DATE_FORM = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static final String SERVER_DATE_FORM1 = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static final String SERVER_DATE_FORM2 = "yyyy-MM-dd'T'HH:mm:ss";
    private static final String DATE_FORM_LOCAL1 = "yyyy.MM.dd HH:mm";
    private static final String DATE_FORM_LOCAL3 = "yyyy.MM.dd";

    private static final String DATE_FORM_LOCAL2 = "yyyy.MM.dd";

    public static String getDate(Date date) {
        return getDate(date, DATE_FORM_LOCAL1);
    }

    public static String getDateType2(Date date) {
        return getDate(date, DATE_FORM_LOCAL3);

    }

    public static String getDate(Date date, String dateForm) {
        DateFormat dateFormat = new SimpleDateFormat(dateForm, Locale.CHINA);
        return dateFormat.format(date);
    }


    /**
     * 判断传入的时间是否是今天
     *
     * @param dateFrom
     * @return
     */

    public static boolean dateIsToday(Date dateFrom) {
       /* DateFormat dateFormat = new SimpleDateFormat(DATE_FORM_LOCAL2, Locale.CHINA);
        String currentDate = dateFormat.format(new Date());
        String reviewDate = dateFormat.format(date);
        return reviewDate.equals(currentDate);*/

        Calendar c1 = Calendar.getInstance();
        c1.setTime(dateFrom);
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);

        return (year1 == year2 && month1 == month2 && day1 == day2);
    }

    /**
     * 判断传入的时间是否是昨天
     * *@param date
     *
     * @return
     */
    public static boolean dateIsYestoday(Date dateFrom) {
       /* Date currentDate = new Date();
        long currentDayInMilliSecond = currentDate.getTime()%(1000 * 60 * 24);
        long fromDayMilliSecond = dateFrom.getTime() % (1000 * 60 * 24);
        return currentDayInMilliSecond == fromDayMilliSecond + (1000 * 60 * 24);*/
        // DateFormat dateFormat = new SimpleDateFormat(DATE_FORM_LOCAL2, Locale.CHINA);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(dateFrom);
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);

        return (year1 == year2 && month1 == month2 && day1 + 1 == day2);

    }

    /**
     * 传入秒获取时分秒
     *
     * @param duration
     */
    public static String[] getHHmmssFromSeconds(int duration) {
        String[] result = new String[3];
        String ss;
        String mm;
        String hh;
        int s = duration % 60;
        int m = ((duration - s) % 3600) / 60;
        int h = (duration - s - m * 60) / 3600;
        if (s < 10) {
            ss = "0" + s;
        } else {
            ss = String.valueOf(s);
        }

        if (m < 10) {
            mm = "0" + m;
        } else {
            mm = String.valueOf(m);
        }

        if (h < 10) {
            hh = "0" + h;
        } else {
            hh = String.valueOf(h);
        }

        result[0] = hh;
        result[1] = mm;
        result[2] = ss;
        return result;
    }

    public static String getTimeType1FromMillis(int millis) {
        SimpleDateFormat format = new SimpleDateFormat("mm:ss", Locale.CHINA);
        Date date = new Date(millis);
        return format.format(date);
    }

    public static String getTime(int second) {

        if (second < 10) {

            return "00:0" + second;

        }

        if (second < 60) {

            return "00:" + second;

        }

        if (second < 3600) {

            int minute = second / 60;

            second = second - minute * 60;

            if (minute < 10) {

                if (second < 10) {

                    return "0" + minute + ":0" + second;

                }

                return "0" + minute + ":" + second;

            }

            if (second < 10) {

                return minute + ":0" + second;

            }

            return minute + ":" + second;

        }

        int hour = second / 3600;

        int minute = (second - hour * 3600) / 60;

        second = second - hour * 3600 - minute * 60;

        if (hour < 10) {

            if (minute < 10) {

                if (second < 10) {

                    return "0" + hour + ":0" + minute + ":0" + second;

                }

                return "0" + hour + ":0" + minute + ":" + second;

            }

            if (second < 10) {

                return "0" + hour + minute + ":0" + second;

            }

            return "0" + hour + minute + ":" + second;

        }

        if (minute < 10) {

            if (second < 10) {

                return hour + ":0" + minute + ":0" + second;

            }

            return hour + ":0" + minute + ":" + second;

        }

        if (second < 10) {

            return hour + minute + ":0" + second;

        }

        return hour + minute + ":" + second;

    }

    /*
     * add by wangguolong 2019.01.14
     * */
    // a integer to xx:xx:xx
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00分00秒";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    /**
     * 将String类型的时间转换成long,如：12:01:08
     *
     * @param strTime String类型的时间
     * @return long类型的时间
     */
    public static long convertStrTimeToLong(String strTime) {
        String[] timeArry = strTime.split(":");
        long longTime = 0;
        if (timeArry.length == 2) {//如果时间是MM:SS格式
            longTime = Integer.parseInt(timeArry[0]) * 1000 * 60 + Integer.parseInt(timeArry[1]) * 1000;
        } else if (timeArry.length == 3) {//如果时间是HH:MM:SS格式
            longTime = Integer.parseInt(timeArry[0]) * 1000 * 60 * 60 + Integer.parseInt(timeArry[1])
                    * 1000 * 60 + Integer.parseInt(timeArry[0]) * 1000;
            Log.e("convertStrTimeToLong", "=====longTime========" + longTime);
        }
        Log.e("convertStrTimeToLong", "=====SystemClock.elapsedRealtime()========" + SystemClock.elapsedRealtime());
        return SystemClock.elapsedRealtime() - longTime;
    }

    public static Long convert2Minute(int day, int month, int hour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
        if (day != 0) {
            calendar.add(Calendar.DAY_OF_MONTH, day);
        }
        if (month != 0) {
            calendar.add(Calendar.MONTH, month);
        }
        calendar.add(Calendar.HOUR_OF_DAY, hour);
//        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTimeInMillis();
    }

    public static Long convert2Second(int day, int month, int hour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
        if (day != 0) {
            calendar.add(Calendar.DAY_OF_MONTH, day);
        }
        if (month != 0) {
            calendar.add(Calendar.MONTH, month);
        }
        calendar.add(Calendar.HOUR_OF_DAY, hour);
//        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTimeInMillis() / 1000;
    }


}
